﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Develop.DTO.DevelopProjectCustomer;
using AnQinFrameWorkAPI.Entity.Develop.Entity;
using AnQinFrameWorkAPI.Entity.Develop.View.DevelopProjectCustomer;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.JWT;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.Develop;

/// <summary>
///     开发管理-客户管理业务层
/// </summary>
public class DevelopProjectCustomerService
{
    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //请求人信息
    private readonly IUserContext _userContext;

    //构造注入
    public DevelopProjectCustomerService(IUserContext userContext, DatabaseContext context, IMapper mapper)
    {
        _userContext = userContext;
        _context = context;
        _mapper = mapper;
    }

    #region 获取客户管理列表

    public async Task<RequestResponseModel> GetCustomerList(
        DevelopProjectCustomerSearchDTO developProjectCustomerSearchDTO)
    {
        //筛选
        var query = _context.DevelopProjectCustomer
            .ProjectTo<DevelopProjectCustomerView>(_mapper.ConfigurationProvider)
            .AsNoTracking().WhereIF(a => a.Code.Contains(developProjectCustomerSearchDTO.Code),
                !string.IsNullOrEmpty(developProjectCustomerSearchDTO.Code))
            .WhereIF(a => a.Name.Contains(developProjectCustomerSearchDTO.Name),
                !string.IsNullOrEmpty(developProjectCustomerSearchDTO.Name))
            .WhereIF(a => a.Phone.Contains(developProjectCustomerSearchDTO.Phone),
                !string.IsNullOrEmpty(developProjectCustomerSearchDTO.Phone))
            .WhereIF(a => a.CreateTime > developProjectCustomerSearchDTO.StartTime,
                developProjectCustomerSearchDTO.StartTime != null)
            .WhereIF(a => a.CreateTime < developProjectCustomerSearchDTO.EndTime,
                developProjectCustomerSearchDTO.EndTime != null)
            .Where(a => a.IsDelete == false);

        //返回列表
        var developCustomerList = await query
            .OrderBy(a => a.CreateTime)
            .Page(developProjectCustomerSearchDTO.PageIndex, developProjectCustomerSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<DevelopProjectCustomerView>(developCustomerList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增项目管理客户

    public async Task<RequestResponseModel> AddCustomer(DevelopProjectCustomerAddDTO developProjectCustomerAddDTO)
    {
        //校验员工
        var staff = await Valid.ValidId(_context.Staff, _userContext.UserId);
        if (staff == null) return RequestResponse.BadRequest("员工不存在");
        
        //生成新数据
        var customer = _mapper.Map<DevelopProjectCustomerModel>(developProjectCustomerAddDTO);

        customer.Id = Guid.NewGuid();
        customer.CreateStaffId = staff.Id.ToString();
        customer.CreateStaffName = staff.Name;
        customer.IsDelete = false;
        customer.CreateTime = DateTime.Now;

        //新增数据
        _context.DevelopProjectCustomer.Add(customer);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(customer.Id, "新增成功");
    }

    #endregion

    #region 更新项目管理客户

    public async Task<RequestResponseModel> PutCustomer(DevelopProjectCustomerUpdateDTO developProjectCustomerUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.DevelopProjectCustomer, developProjectCustomerUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("客户Id无效");

        //编辑数据
        var customer = _mapper.Map(developProjectCustomerUpdateDTO, query);

        //更新表
        _context.DevelopProjectCustomer.Update(customer);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(developProjectCustomerUpdateDTO.Id);
    }

    #endregion

    #region 删除项目管理客户

    public async Task<RequestResponseModel> DeleteCustomer(SingleIdDTO singleIdDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.DevelopProjectCustomer, singleIdDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("客户Id无效");
        ;

        query.IsDelete = true;

        //删除操作
        _context.DevelopProjectCustomer.Update(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(singleIdDTO.Id, "删除成功");
    }

    #endregion

    #region 批量删除项目管理客户

    public async Task<RequestResponseModel> DeleteArrayCustomer(IdArrayDTO idArrayDTO)
    {
        //校验单据
        var query = await _context.DevelopProjectCustomer
            .WhereIF(a => idArrayDTO.Ids.Contains(a.Id.ToString()), idArrayDTO != null)
            .ToListAsync();

        if (query.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("客户Id无效");

        foreach (var item in query) item.IsDelete = true;
        ;

        //删除并保存
        _context.DevelopProjectCustomer.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO.Ids, "删除成功");
    }

    #endregion
}